grammar DarwinBotsGrammar2;

options {
	output=AST; ASTLabelType=CommonTree;
}

@parser::header{ 
	package org.jdarwinbots.io.dsl; 
	import org.jdarwinbots.logic.*;
}

@parser::members {
	private DNA dna = new DNA();
	
}

@lexer::header{ package org.jdarwinbots.io.dsl;  }

dna 	returns [DNA value]:  (declaration|op)* {$value = dna;}; 

op 	:	(i=INT {dna.addOperator(Integer.parseInt(i.getText()));}) 
		| (r=reference {dna.addAliasReferenceOperator(r.stop.getText());})
		| (m=memloc {dna.addAliasOperator(m.stop.getText());}) 
		| (c=command {dna.addOperator(c.start.getText());}); 

memloc : '.' ID;

COMMENT : '\''  ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;};
  
declaration 	:	 'def'  n = ID  v = INT {dna.addAlias(n.getText(),Integer.parseInt(v.getText()));};

reference 	:	 '*.' ID ;

command :	'cond' | 'start' | 'else' | 'stop' | 'end' | 'store' | 'inc' | 'dec' | 'add' | 'sub' |
'mult' | 'div' | 'rnd' | 'mod' | 'sgn' | 'abs' | 'dup' | 'angle' | 'dist' | 'ceil' | 'floor' | 'sqr' |
'pow' | 'pyth' | '<' | '>' | '<=' | '>=' | '=' | '!=' | '%=' | '!%=' | '~=' | '!~=' | 'and' | 'or' |
'not' | 'xor' | 'dropbool' | 'dupbool' | 'swapbool' | 'overbool' | 'drop' | 'swap' | 'over' | '~' | '|' |
'&' | '^' | '++' | '--' | '-' | '<<' | '>>' | 'clearbool' | 'clearint' | 'true' | 'false';

ID  :	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

INT :	'0'..'9'+
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;


fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

fragment
OCTAL_ESC
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;
    
